package TAIC.feature;

import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Scanner;

/**
 * This program extraxct points in the first image in the imagelist 
 * @author Kent
 *
 */

public class PointInImage {
	static public void main ( String args[ ]) throws Exception {
		if ( args.length != 3 ){ 
			System.out.println ( "please input folder name, which image you want to mark \r\n " +
					"and number of cluster you want to show") ;
			return  ; 
		}
		( new PointInImage ()).work( args ); 
	}
	
	public void work ( String args [] ) throws Exception {
		String folder = args[ 0 ] ;
		int nImage = Integer.valueOf( args [ 1 ]) ;
		int topCluster = Integer.valueOf( args[ 2 ]) ;
		
		Scanner scanner = new Scanner ( new File ( folder + "\\index.txt" )) ; 
		int count = 0 ;
		int nFeature = 0 ; 
		int beginPos = 0 ;
		String imgFileName = null ; 
		for ( int i = 0 ; i < nImage; i ++ ) {
			beginPos += nFeature; 
			imgFileName = scanner.nextLine().trim() ;
			Scanner fScanner = new Scanner ( new File ( folder + "\\" + imgFileName + ".sift_o" )) ;
			nFeature = fScanner.nextInt() ;
			fScanner.close() ;
		}
		scanner.close();
		
		HashMap < Integer, Integer > map = new HashMap < Integer, Integer > () ;  
		scanner = new Scanner ( new File ( folder + "\\mat.stat"))  ;
		for ( int i = 0 ; i < topCluster; i ++ ) {
			map.put( scanner.nextInt() ,  i ) ;
			scanner.nextLine() ;
		}		
		scanner.close() ;
		
		PrintStream fout = new PrintStream ( folder + "\\" + imgFileName + ".point" ) ;
		fout.println ( imgFileName ) ; 
		scanner = new Scanner ( new File ( folder + "\\" + imgFileName + ".sift_o")) ;
		Scanner cluScan = new Scanner ( new File (folder + "\\mat.clustering.400" )) ;
		cluScan.nextLine () ;
		System.out.println ( beginPos + " " + nFeature ) ; 
		for ( int i = 0 ; i < beginPos; i ++ ) cluScan.nextLine() ;
		for ( int i = 0 ; i < nFeature ; i ++ ) {
			Integer color = map.get(cluScan.nextInt());
			if ( color != null ) fout.println ( scanner.next() + " " + scanner.next() + " " + color ) ; 
			scanner.nextLine() ;
		}
		
		cluScan.close();
		scanner.close() ;
		fout.close() ;
	}
}
